<?php

$file_id = $path[3];

if (ctype_digit($file_id)) {
	
	$file = $db->fetchRow("SELECT * FROM cms_files WHERE site_id=? AND id=?", array($site['id'], $file_id));
	list($file['filename_name'], $file['filename_ext']) = preg_extract("/^(.*)\.([^\.]+)/", $file['filename']);
	
	if ($_POST) {
		if ($_POST['file']['filename_name']) {
			$file = $_POST['file'];
			$file['filename'] = $file['filename_name'] . $file['filename_ext'];
			unset($file['filename_name']);
			unset($file['filename_ext']);
			
			$db->update('cms_files', array_merge($file, array('modified' => new Zend_Db_Expr('NOW()'))), "id={$file_id}");
			$smarty->assign('success', true);
		} else{
			$smarty->assign('errors', array('FILENAME'));
		}
	}
	
	$smarty->assign('file', $file);
	
	if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
		header('Content-type: text/xml');
		$smarty->display('global/files/_edit_messages.tpl.taconite');
	} else {
		$smarty->display('global/files/edit.tpl.html');
	}
	
} else {
	header("Location: /admin/global/files/");
}